package com.mql.ganio.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mql.ganio.api.base.BaseApiRetrofit;
import com.mql.ganio.model.request.LoginRequest;
import com.mql.ganio.model.response.EveryDayResponse;
import com.mql.ganio.model.response.FrontpageResponse;
import com.mql.ganio.model.response.GanIoResponse;
import com.mql.ganio.model.response.LoginResponse;
import com.mql.ganio.util.LogUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
 * @创建者 CSDN_LQR
 * @描述 使用Retrofit对网络请求进行配置
 */
public class ApiRetrofit extends BaseApiRetrofit {

    public MyApi mApi;
    public static ApiRetrofit mInstance;

    private ApiRetrofit() {
        super();


        //在构造方法中完成对Retrofit接口的初始化

    }
    private  Gson getGson(){
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        return gson;
    }
    public MyApi getLoginApi(){
        mApi = new Retrofit.Builder()
                .baseUrl(MyApi.LOGIN_URL)
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create(getGson()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(MyApi.class);
        return mApi;
    }

    public MyApi getGanIoApi(){
        mApi = new Retrofit.Builder()
                .baseUrl(MyApi.GANIO_URL)
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create(getGson()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(MyApi.class);
        return mApi;
    }

    public MyApi getBannerApi(){
        mApi = new Retrofit.Builder()
                .baseUrl(MyApi.API_TING)
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create(getGson()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(MyApi.class);
        return mApi;
    }

    public static ApiRetrofit getInstance() {
        if (mInstance == null) {
            synchronized (ApiRetrofit.class) {
                if (mInstance == null) {
                    mInstance = new ApiRetrofit();
                }
            }
        }
        return mInstance;
    }

    private RequestBody getRequestBody(Object obj) {
        String route = new Gson().toJson(obj);

        Map<String, Object> map = new HashMap<>();
        map.put("body", route);
        String s = map.toString();
        LogUtils.sf(s.substring(1, s.length() - 1));
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), s.substring(1, s.length() - 1));
//        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), route);
        return body;
    }


    //登录
    public Observable<LoginResponse> login(String phone, String password) {

        return getLoginApi().login(getRequestBody(new LoginRequest(phone, password)));
    }

    //干货
    public Observable<GanIoResponse> getGanIoData(String type, int page, int pre_page) {
        return getGanIoApi().getGanIoData(type, page, pre_page);
    }

    //每日推荐
    public Observable<EveryDayResponse> getEveryDayData(String year,String month,String day){
        return getGanIoApi().getGankIoDay(year,month,day);
    }

    //轮播图
    public Observable<FrontpageResponse> getBannerData(){
        return  getBannerApi().getFrontpage();
    }
}
